home *** CD-ROM | disk | FTP | other *** search
/ MacTech 1 to 12 / MacTech-vol-1-12.toast / Source / MacTech® Magazine / Volume 12 - 1996 / 12.12 Dec 96 / Async I⁄O Code / Sources / scanners.c < prev    next >
Encoding:
Text File  |  1996-03-13  |  4.7 KB  |  197 lines  |  [TEXT/CWIE]

  1. // File: Scanners.h
  2. //
  3. // Sources for each of our character counting routines
  4. //
  5.  
  6. #include "scanners.h"
  7. #ifndef __LOWMEM__
  8.     #include <LowMem.h>
  9. #endif
  10.  
  11.  
  12. static int     kBufferSize = 128 * 1024;
  13.  
  14. // --------------------------------------------------------------------------------
  15.  
  16. int countChars(FSSpecPtr fsp)
  17. {
  18.   // Count the number of times the letter
  19.   // A appears in the file
  20.   FILE  *f = NULL;
  21.   int    counter = 0;
  22.   char    currChar;
  23.   char    filename[64];
  24.   
  25.   BlockMove((Ptr)&fsp->name[1], filename, fsp->name[0]);
  26.   filename[fsp->name[0]] = '\0';
  27.   
  28.   f = fopen(filename, "r");
  29.   while ((currChar = fgetc(f)) != EOF) {
  30.       if (currChar == 'A') counter += 1;
  31.   }
  32.   fclose(f);
  33.   return counter;
  34. }
  35.  
  36. // --------------------------------------------------------------------------------
  37.  
  38. int countCharsFS(FSSpecPtr fsp)
  39. {
  40.     // Count the number of times the letter
  41.     // A appears in the file reading the file in
  42.     // blocks
  43.     int        counter = 0;
  44.     char    *buffer, *currChar;
  45.     short    refNum;
  46.     long    charCount;
  47.     OSErr    err;
  48.     
  49.     err = FSpOpenDF(fsp, fsRdPerm, &refNum); 
  50.     if (err == noErr) {
  51.         buffer = (char*)NewPtr(kBufferSize);
  52.         if (buffer != nil) {
  53.             for (;;) {
  54.                 charCount = kBufferSize;
  55.                 err = FSRead(refNum, &charCount, (Ptr)buffer);
  56.                 if ((err != noErr) && (err != eofErr)) break;
  57.                 if (charCount == 0) break;
  58.                 currChar= buffer;
  59.                 while (charCount-- > 0) {
  60.                   if (*currChar++ == 'A') counter++;
  61.                 }
  62.             }
  63.                DisposePtr(buffer);
  64.         }
  65.         FSClose(refNum);
  66.     }
  67.     return counter;
  68. }
  69.  
  70. // --------------------------------------------------------------------------------
  71.  
  72. int countCharsFS2(FSSpecPtr fsp)
  73. {
  74.     // Count the number of times the letter
  75.     // A appears in the file after reading the
  76.     // whole file into memory
  77.     int    counter = 0;
  78.     char    *buffer, *currChar;
  79.     Handle    hBuffer = nil;
  80.     short    refNum;
  81.     long    charCount;
  82.     OSErr    err;
  83.     
  84.     err = FSpOpenDF(fsp, fsRdPerm, &refNum); 
  85.     if (err == noErr) {
  86.         // get the file size, set up an area to hold it all,
  87.         // and read the file
  88.         (void) GetEOF(refNum, &charCount);
  89.         
  90.         // Decide where to allocate the memory
  91.         // We'll use temporary memory if there's not enough
  92.         // room in the main memory
  93.         if (FreeMem() >= charCount)
  94.             hBuffer = NewHandle(charCount);
  95.         else if ((hBuffer == nil) && (TempFreeMem()  >= charCount))
  96.             hBuffer = TempNewHandle(charCount, &err);
  97.             
  98.         if (hBuffer != nil) {
  99.             HLock(hBuffer);
  100.             buffer = (char*)*hBuffer;
  101.             (void) FSRead(refNum, &charCount, (Ptr)buffer);
  102.         }
  103.         FSClose(refNum);
  104.         
  105.         if (buffer != NULL) {
  106.             currChar= buffer;
  107.             while (charCount-- > 0) {
  108.               if (*currChar++ == 'A') counter++;
  109.             }
  110.                DisposeHandle(hBuffer);
  111.         }
  112.     }
  113.     return counter;
  114. }
  115.  
  116. // --------------------------------------------------------------------------------
  117.  
  118. void setup (ParmBlkPtr pb, short refNum, short vRefNum, long bufSize);
  119. void destroy (ParmBlkPtr pb);
  120.  
  121. void setup (ParmBlkPtr pb, short refNum, short vRefNum, long bufSize)
  122. {
  123.     pb->ioParam.ioCompletion = NULL;
  124.     pb->ioParam.ioResult = 1;
  125.     pb->ioParam.ioRefNum = refNum;
  126.     pb->ioParam.ioVRefNum = vRefNum;
  127.     pb->ioParam.ioReqCount = bufSize;
  128.     pb->ioParam.ioBuffer = NewPtr(bufSize);
  129.     pb->ioParam.ioPosMode = fsFromMark;
  130.     pb->ioParam.ioPosOffset = 0;
  131. }
  132.  
  133. void destroy (ParmBlkPtr pb)
  134. {
  135.     DisposePtr(pb->ioParam.ioBuffer);
  136.     DisposePtr((Ptr)pb);
  137. }
  138.  
  139. int countCharsAsync(FSSpecPtr fsp)
  140. {
  141.   // Count the number of times the letter
  142.   // A appears in the file, reading the file
  143.   // one character at a time
  144.   int            counter = 0;
  145.   ParmBlkPtr    pb[2], currPBPtr;
  146.   int            currPB = 0;
  147.   char            *buffer, *currChar;
  148.   short            refNum;
  149.   long            charCount;
  150.   OSErr            err;
  151.   
  152.     // Allocate parameter blocks
  153.     // Open the file
  154.     err = FSpOpenDF(fsp, fsRdPerm, &refNum); 
  155.     if (err == noErr) {
  156.         // Set up parameter blocks
  157.         pb[0] = (ParmBlkPtr)NewPtrClear(sizeof(ParamBlockRec));
  158.         pb[1] = (ParmBlkPtr)NewPtrClear(sizeof(ParamBlockRec));
  159.         setup(pb[0], refNum, fsp->vRefNum, kBufferSize);
  160.         setup(pb[1], refNum, fsp->vRefNum, kBufferSize);
  161.       
  162.         // Start 2 read operations going
  163.         (void) PBReadAsync(pb[0]);    
  164.         (void) PBReadAsync(pb[1]);    
  165.         currPBPtr = pb[0];
  166.         
  167.         for (;;) {
  168.             // Wait for the I/O operation to complete
  169.             while (currPBPtr->ioParam.ioResult > 0) {};
  170.             
  171.             // The data is ready, so count the characters
  172.             buffer = currPBPtr->ioParam.ioBuffer;
  173.             charCount = currPBPtr->ioParam.ioActCount;
  174.             if (charCount == 0) break;
  175.             currChar= buffer;
  176.             while (charCount-- > 0) {
  177.               if (*currChar++ == 'A') counter++;
  178.             }
  179.             
  180.             // Put this buffer back into the reading queue
  181.             (void) PBReadAsync(currPBPtr);
  182.             
  183.             // Switch to the other buffer
  184.             currPB = 1 - currPB;
  185.             currPBPtr = pb[currPB];
  186.             currPBPtr->ioParam.ioPosMode = fsAtMark;
  187.         }
  188.         KillIO(refNum);
  189.         destroy(pb[0]);
  190.         destroy(pb[1]);
  191.         FSClose(refNum);
  192.     }
  193.     return counter;
  194. }
  195.  
  196.  
  197.